package com.app.zenmewan.controller;

import com.app.zenmewan.entity.Comment;
import com.app.zenmewan.entity.Jubao;
import com.app.zenmewan.entity.User;
import com.app.zenmewan.service.CommentService;
import com.app.zenmewan.service.JubaoService;
import com.app.zenmewan.service.UserService;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (Jubao)表控制层
 *
 * @author makejava
 * @since 2020-05-24 20:58:45
 */
@RestController
@RequestMapping("jubao")
public class JubaoController {
    /**
     * 服务对象
     */
    @Resource
    private JubaoService jubaoService;
    @Resource
    private CommentService commentService;
    @Resource
    private UserService userService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Jubao selectOne(Integer id) {
        return this.jubaoService.queryById(id);
    }

    @PostMapping("insert")
    public void insert(@RequestBody Jubao jubao) {
        System.out.println("jubao has driven");
        this.jubaoService.insert(jubao);
    }

    @Getter
    @Setter
    class Vo {
        Jubao jubao;
        String comment;
        User user;
        boolean iscomment;
    }

    @GetMapping("list")
    public List<Vo> list() {
        List<Jubao> list = this.jubaoService.queryAllByLimit(0, 9999);
        List<Vo> vos = new ArrayList<>();
        for (Jubao jubao : list) {
            Vo v = new Vo();
            if (jubao.getTypetext().equals("评论内容不合法")) {
                v.setIscomment(true);
                v.setComment(this.commentService.queryById(jubao.getCid()).getContent());
            }else{
                v.setIscomment(false);
                v.setComment("无");
            }
            v.setUser(this.userService.queryById(jubao.getUid()));
            v.getUser().setToken("");
            v.getUser().setPassword(null);

            v.setJubao(jubao);
            vos.add(v);
        }
        return vos;
    }

    /**
     * 删除评论内容以及依赖于这评论的回复评论内容,还有举报这个相关评论的内容
     *
     * @param id
     */
    @GetMapping("delete")
    public void delete(int id) {
        int cid = this.jubaoService.queryById(id).getCid();
        this.jubaoService.deleteById(id);
        Jubao j = new Jubao();
        j.setCid(cid);
        for (Jubao jubao : this.jubaoService.queryAll(j)) {
            this.jubaoService.deleteById(jubao.getId());
        }
        Comment c = new Comment();
        c.setToId(cid);
        for (Comment comment : this.commentService.queryAll(c)) {
            this.commentService.deleteById(comment.getId());
        }
        this.commentService.deleteById(cid);
    }
    @GetMapping("dongjie")
    public void dongjie(int id){
        int uid = this.jubaoService.queryById(id).getUid();
        this.jubaoService.deleteById(id);
        this.userService.Dongjie(uid);
    }
    @GetMapping("hulve")
    public void hulve(int id) {
       this.jubaoService.deleteById(id);
    }

}